/**
 * author: MaHX
 * date: 2019/8/18/22:57
 * description: 自定义LinkList
 **/
public class MyLikeList {
    Node head = null;
    int size = 0;

    public int size() {
        return size;
    }

    public void add(Object value) {
        Node newNode = new Node(value);

//        判断是否为第一次插入
        if (head == null) {  //第一次插入
            head = newNode;
        } else {
            Node temp = head;
            //寻找最后一个节点
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            temp.setNext(newNode);
        }
        size++;
    }

    public void set(int index, Object value) {
        Node temp = head;
        //寻找下标为index的节点
        for (int i = 0; i < index ; i++) {
            temp = temp.getNext();
        }
        temp.setValue(value);
    }

    public Object get(int index) {
        Node temp = head;
        //寻找下标为index的节点
        for (int i = 0; i < index ; i++) {
            temp = temp.getNext();
        }
        return  temp.getValue();
    }

    public void clear() {
        head = null;
        size = 0;
    }

    public void removeAt(int index) {
        Node temp = head;
        //寻找下标为index的前一个节点
        for (int i = 0; i < index-1; i++) {
            temp = temp.getNext();
        }
        temp.setNext(temp.getNext().getNext());
        size--;
    }
}
